from django.db import models


# 基类
class BaseModel(models.Model):
    create_time = models.DateTimeField("创建时间", auto_now_add=True, null=True, blank=True, )
    update_time = models.DateTimeField("更新时间", auto_now=True, null=True, blank=True, )
    is_delete = models.SmallIntegerField("逻辑删除 1删除", default=0)

    class Meta:
        abstract = True


# 角色表   # 学生# 老师# 学校负责人# 超级管理员
class RoleModel(BaseModel):
    rname = models.CharField("角色名称", max_length=11)
    remark = models.CharField('备注', max_length=128, null=True, blank=True)

    class Meta:
        db_table = 'tb_role'

    def __str__(self):
        return self.rname


# # 权限表
# class AuthorityModel(BaseModel):
#     aname = models.CharField("权限名称", max_length=60)
#     path = models.CharField("权限详情", max_length=200, null=True, blank=True)
#     remark = models.CharField("备注", max_length=32, null=True, blank=True)
#
#     class Meta:
#         db_table = "tb_auth"
#         verbose_name_plural = '权限表'
#
#     def __str__(self):
#         return self.aname


# 菜单
class MenuModel(BaseModel):
    # 菜单名称 菜单路径 排序 图标 类型
    mname = models.CharField('菜单名称', max_length=33)
    murl = models.CharField('菜单路径', max_length=333, default='')
    sort = models.SmallIntegerField('排序', default=0)
    ask_off = models.CharField('图标', null=True, blank=True, max_length=50)
    type = models.SmallIntegerField('类型', null=True, blank=True, choices=((0, '目录'), (1, '菜单')))
    # sort = models.CharField('排序', null=True, blank=True, max_length=10)  # 1-1
    # 自关联
    parent = models.ForeignKey('self', default=None,null=True,blank=True,on_delete=models.CASCADE)

    class Meta:
        db_table = 'tb_menu'

    def __str__(self):
        return self.mname

# 角色菜单  中间表
class RoleMenuModel(BaseModel):
    role = models.ForeignKey(RoleModel, on_delete=models.CASCADE, verbose_name="角色id", null=True, blank=True, )
    menu = models.ForeignKey(MenuModel, on_delete=models.CASCADE, verbose_name="菜单id", null=True, blank=True, )

    class Meta:
        db_table = 'tb_rolemenu'

    def __str__(self):
        return f'{self.role}的权限(角色菜单表)'



# 学校
class SchoolModel(BaseModel):
    sname = models.CharField("学校名称", max_length=32)
    rector = models.SmallIntegerField('学校负责人', null=True, blank=True)

    class Meta:
        db_table = 'tb_school'
        verbose_name_plural = '学校'

    def __str__(self):
        return self.sname


# 用户表
class UserModel(BaseModel):
    account = models.CharField("账号", max_length=228)
    password = models.CharField("密码", max_length=533)
    creator = models.ForeignKey("self", models.CASCADE, verbose_name="老师创建人(学校负责人", null=True, blank=True)
    role = models.ForeignKey(RoleModel, on_delete=models.CASCADE, verbose_name="角色")
    school = models.ForeignKey(SchoolModel, models.CASCADE, verbose_name="所属学校", null=True, blank=True)

    class Meta:
        db_table = 'tb_user'

    def __str__(self):
        return self.account


# 用户详情表
class UserInfoModel(BaseModel):
    name = models.CharField("姓名", max_length=52)
    phone = models.CharField("手机号", max_length=11, null=True, blank=True, )
    address = models.CharField("地址", max_length=33, null=True, blank=True, )
    birthday = models.DateField("生日", null=True, blank=True, )
    head_img = models.CharField("头像", max_length=733, null=True, blank=True, )
    user = models.ForeignKey(UserModel, on_delete=models.CASCADE, verbose_name="用户id")

    class Meta:
        db_table = 'tb_userinfo'

    def __str__(self):
        return self.name


# 登录日志
class LoginLogModel(BaseModel):
    login_ip = models.CharField('登录ip', max_length=32)
    account = models.CharField('账号名称', max_length=32)
    device = models.CharField('登录设备', max_length=250)
    address = models.CharField('登录地区', max_length=32)
    user = models.ForeignKey(UserModel, on_delete=models.CASCADE, verbose_name="登录的用户")

    class Meta:
        db_table = 'tb_login'
        verbose_name_plural = '登录日志'

    def __str__(self):
        return self.account


# 操作日志
class OperateLogModel(BaseModel):
    user = models.ForeignKey(UserModel, on_delete=models.CASCADE, verbose_name='操作人')
    path = models.CharField('操作路径', max_length=64)
    action = models.CharField('操作动作', max_length=32)

    class Meta:
        db_table = 'tb_operate'
        verbose_name_plural = '操作日志'

    def __str__(self):
        return self.action


# 班级
class ClassModel(BaseModel):
    cname = models.CharField("班级名称", max_length=32)
    school = models.ForeignKey(SchoolModel, models.CASCADE, verbose_name="所属学校")

    class Meta:
        db_table = 'tb_grade'
        verbose_name_plural = '班级'

    def __str__(self):
        return self.cname


# 老师与班级三方表
class ClassTeacher(BaseModel):
    teacher = models.ForeignKey(UserModel, models.CASCADE, verbose_name="老师")
    classs = models.ForeignKey(ClassModel, models.CASCADE, verbose_name="所属班级")

    class Meta:
        db_table = 'tb_teacher'
        verbose_name_plural = '老师和班级'

    def __str__(self):
        return self.teacher


# 学生表
class ClassStudent(BaseModel):
    student = models.ForeignKey(UserModel, models.CASCADE, verbose_name="学生")
    classs = models.ForeignKey(ClassModel, models.CASCADE, verbose_name="所属班级 ")

    class Meta:
        db_table = 'tb_student'
        verbose_name_plural = '学生'

    def __str__(self):
        return self.student


# 问候语表
class GreetingsModel(BaseModel):
    content = models.CharField("问候语内容", max_length=100)
    start = models.SmallIntegerField('开始时间')
    end = models.SmallIntegerField('结束时间')

    class Meta:
        db_table = 'tb_greetings'
        verbose_name_plural = '问候语'

    def __str__(self):
        return self.content


# 节假日问候语表


# 分类表
class CateModel(BaseModel):
    cname = models.CharField("分类名称", max_length=32)
    sequence = models.SmallIntegerField("展示顺序")
    parent = models.ForeignKey('self', on_delete=models.CASCADE, verbose_name='所属分类', null=True, blank=True)

    class Meta:
        db_table = 'tb_cate'
        verbose_name_plural = '分类表'

    def __str__(self):
        return self.cname


# 标签表
class TagModel(BaseModel):
    tname = models.CharField("标签名", max_length=32)
    sequence = models.SmallIntegerField("展示顺序")
    owner = models.ForeignKey(UserModel, on_delete=models.CASCADE, verbose_name="作者，拥有者")

    class Meta:
        db_table = 'tb_tag'
        verbose_name_plural = '标签表'

    def __str__(self):
        return self.tname


# 学习内容
class LearnContentModel(BaseModel):
    title = models.CharField("标题", max_length=50)
    text = models.TextField("内容", max_length=10000)
    # course_img = models.CharField('封面', max_length=258, null=True, blank=True)
    is_show = models.BooleanField("是否公开", default=True)
    cate = models.ForeignKey(CateModel, on_delete=models.CASCADE, verbose_name="分类id")

    class Meta:
        db_table = 'tb_learn_content'
        verbose_name_plural = '学习内容'

    def __str__(self):
        return self.title


# 标签和学习内容三方表
class TagLearnModel(BaseModel):
    tag = models.ForeignKey(TagModel, on_delete=models.CASCADE, verbose_name="标签")
    content = models.ForeignKey(LearnContentModel, on_delete=models.CASCADE, verbose_name="学习内容")

    class Meta:
        db_table = 'tb_tag_learn'
        verbose_name_plural = '内容和标签'

    def __str__(self):
        return self.tag


# 首页轮播图
class ShowImgModel(BaseModel):
    img = models.CharField("图片地址", max_length=258)
    is_show = models.BooleanField("是否展示", default=False)

    class Meta:
        db_table = 'tb_show_img'
        verbose_name_plural = '首页图片'

    def __str__(self):
        return self.img


# 收藏学习内容
class CollectModel(BaseModel):
    user = models.ForeignKey(UserModel, on_delete=models.CASCADE, verbose_name="收藏人")
    content = models.ForeignKey(LearnContentModel, on_delete=models.CASCADE, verbose_name="收藏的学习内容")

    class Meta:
        db_table = 'tb_collect'
        verbose_name_plural = '收藏学习内容'

    def __str__(self):
        return self.content


# 学习任务
class LearnTaskModel(BaseModel):
    content = models.ForeignKey(LearnContentModel, on_delete=models.CASCADE, verbose_name="学习内容")
    remark = models.CharField('备注', max_length=128, null=True, blank=True)
    classs = models.ForeignKey(ClassModel, on_delete=models.CASCADE, verbose_name="班级")
    operator = models.ForeignKey(UserModel, on_delete=models.CASCADE, verbose_name="操作人/老师")

    class Meta:
        db_table = 'tb_lear_task'
        verbose_name_plural = '学习任务'

    def __str__(self):
        return self.content


# 学习记录  TODO 艾宾浩斯记忆曲线
class LearnRecordModel(BaseModel):
    user = models.ForeignKey(UserModel, on_delete=models.CASCADE, verbose_name="学习人")
    task = models.ForeignKey(LearnTaskModel, on_delete=models.CASCADE, verbose_name="任务")
    learn_time = models.IntegerField("学习时长(单位分钟)", default=0)
    level = models.SmallIntegerField('学习程度 0 生疏 1掌握', )

    class Meta:
        db_table = 'tb_lear_record'
        verbose_name_plural = '学习记录'

    def __str__(self):
        return self.user


# 学习资源
class LearnResourceModel(BaseModel):
    title = models.CharField("资源名", max_length=32)
    img = models.CharField("封面图", max_length=532)
    text = models.CharField("资源介绍", max_length=532)
    price = models.DecimalField("价格", max_digits=7, decimal_places=2)
    cate = models.ForeignKey(CateModel, on_delete=models.CASCADE, verbose_name="分类id")

    class Meta:
        db_table = 'tb_lear_resource'
        verbose_name_plural = '学习资源'

    def __str__(self):
        return self.title


# 标签和资源三方表
class TagResourceModel(BaseModel):
    tag = models.ForeignKey(TagModel, on_delete=models.CASCADE, verbose_name="标签")
    resource = models.ForeignKey(LearnResourceModel, on_delete=models.CASCADE, verbose_name="学习资源")

    class Meta:
        db_table = 'tb_tag_resource'
        verbose_name_plural = '资源和标签'

    def __str__(self):
        return self.tag


# 章节
class ChaptersModel(BaseModel):
    title = models.CharField("章节名", max_length=32)
    sequence = models.SmallIntegerField("展示顺序")
    resource = models.ForeignKey(LearnResourceModel, on_delete=models.CASCADE, verbose_name="资源")
    is_free = models.BooleanField('是否免费', default=False)

    class Meta:
        db_table = 'tb_chapters'
        verbose_name_plural = '章节'

    def __str__(self):
        return self.title


# 小节  TODO 与学习内容关联
class SectionsModel(BaseModel):
    title = models.CharField("小节名", max_length=32)
    sequence = models.SmallIntegerField("展示顺序")
    content = models.TextField("内容", max_length=10000)
    like_count = models.SmallIntegerField("点赞次数", default=0, null=True, blank=True)
    chapter = models.ForeignKey(ChaptersModel, on_delete=models.CASCADE, verbose_name="章节")

    class Meta:
        db_table = 'tb_sections'
        verbose_name_plural = '小节'

    def __str__(self):
        return self.title


# 评论
class CommentsModel(BaseModel):
    user = models.ForeignKey(UserModel, on_delete=models.CASCADE, verbose_name="评论人")
    common_content = models.TextField("评论内容", max_length=400)
    like_count = models.IntegerField("点赞次数", default=0, null=True, blank=True)
    parent_id = models.ForeignKey("self", on_delete=models.CASCADE, verbose_name="被评论的id")
    is_top = models.BooleanField("是否被置顶", default=False, null=True, blank=True)
    # resource = models.ForeignKey(LearnResourceModel, on_delete = models.CASCADE, verbose_name="资源")
    content = models.ForeignKey(LearnContentModel, on_delete=models.CASCADE, verbose_name="学习内容")

    reply_count = models.IntegerField("回复数", default=0, null=True, blank=True)

    class Meta:
        db_table = 'tb_comments'
        verbose_name_plural = '评论'

    def __str__(self):
        return self.common_content

# 点赞评论
class LikeModel(BaseModel):
    user = models.ForeignKey(UserModel, on_delete=models.CASCADE, verbose_name="点赞人")
    comment = models.ForeignKey(CommentsModel, on_delete=models.CASCADE, verbose_name="评论id")

    class Meta:
        db_table = 'tb_like'
        verbose_name_plural = '点赞评论'

    def __str__(self):
        return self.comment

# 用户主页 点赞
class HomeLikeModel(BaseModel):
    like_person = models.ForeignKey(UserModel, on_delete=models.CASCADE, verbose_name="点赞人")
    be_praised = models.SmallIntegerField("被赞人", null=True, blank=True)
    content = models.CharField("点赞内容", max_length=32)
    # content = models.ForeignKey(LearnContentModel, on_delete=models.CASCADE, verbose_name="学习内容")

    class Meta:
        db_table = 'tb_home_like'
        verbose_name_plural = '点赞用户主页'

    def __str__(self):
        return self.like_person


# 搜索记录
class SearchRecordModel(BaseModel):
    user = models.ForeignKey(UserModel, on_delete=models.CASCADE, verbose_name="搜索人")
    content = models.CharField("搜索内容", max_length=56)

    class Meta:
        db_table = 'tb_search_record'
        verbose_name_plural = '搜索记录'

    def __str__(self):
        return self.content


# 消息类型表
class TypesModel(BaseModel):
    tname = models.CharField("消息类型", max_length=32)

    class Meta:
        db_table = 'tb_types'
        verbose_name_plural = '消息类型表'

    def __str__(self):
        return self.tname


# 消息表
class MessageModel(BaseModel):
    receive = models.ForeignKey(UserModel, on_delete=models.CASCADE, verbose_name="接收人")
    send = models.SmallIntegerField("发送人", null=True, blank=True)
    text = models.CharField("消息内容", max_length=532)
    types = models.ForeignKey(TypesModel, on_delete=models.CASCADE, verbose_name="消息类型")

    class Meta:
        db_table = 'tb_message'
        verbose_name_plural = '消息表'

    def __str__(self):
        return self.receive
